package com.foo.designpattern.observer;

/**
 * com.foo.designpattern.observer
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-04-22 17:34.
 *
 * @Description 具体观察者
 */
public class Person implements Observer {
    // 旅行社
    Subject travelSubject = null;
    // 气象站
    Subject weatherSubject = null;
    // 天气预报时间
    String forecastTime = null;
    // 天气状况
    String forecastMess = null;
    // 最高温度
    int maxTemperature = 0;
    // 最低温度
    int minTemperature = 0;
    // 旅游日期
    String tourStartTime = null;
    // 旅游信息
    String tourMess = null;

    /**
     *
     * 这个地方 可用List<T> 方式作为一个函数包括进来
     *
     * @param travelSubject
     * @param weatherSubject
     */
    public Person(Subject travelSubject, Subject weatherSubject) {
        this.travelSubject = travelSubject;
        this.weatherSubject = weatherSubject;
        travelSubject.addObserver(this);
        weatherSubject.addObserver(this);
    }

    /**
     * 观察者更新数据
     *
     * @param subject
     */
    @Override
    public void update(Subject subject) {
        if (subject instanceof WeatherStation) {
            // 更新天气信息
            WeatherStation weatherStation = (WeatherStation) subject;
            forecastMess = weatherStation.getForecastMess();
            forecastTime = weatherStation.getForecastTime();
            maxTemperature = weatherStation.getMaxTemperature();
            minTemperature = weatherStation.getMinTemperature();
            System.out.println("天气预报：" + this.forecastTime + "，天气状况：" + this.forecastMess + "，最高温度：" + this.maxTemperature + "℃，最低温度：" + this.minTemperature + "℃");
        } else if (subject instanceof TravelAgency) {
            // 更新旅游信息
            TravelAgency travelAgency = (TravelAgency) subject;
            this.tourMess = travelAgency.getTourMess();
            this.tourStartTime = travelAgency.getTourStartTime();
            System.out.println("旅游日期：" + this.tourStartTime + "，旅游信息：" + this.tourMess);
        }
    }
}
